<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="策略标识" prop="strategySign">-->
<!--        <el-input-->
<!--          v-model="queryParams.strategySign"-->
<!--          placeholder="请输入策略标识"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="会员金额" prop="levelFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.levelFee"-->
<!--          placeholder="请输入会员金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="币种" prop="currency">
        <el-select v-model="queryParams.currency" placeholder="请选择币种" clearable>
          <el-option
            v-for="dict in dict.type.ora_sys_currency"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="可用状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择可用状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="会员时长" prop="memberTime">-->
<!--        <el-input-->
<!--          v-model="queryParams.memberTime"-->
<!--          placeholder="请输入会员时长"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="位置" prop="position">-->
<!--        <el-input-->
<!--          v-model="queryParams.position"-->
<!--          placeholder="请输入位置"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序" prop="orderBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.orderBy"-->
<!--          placeholder="请输入排序"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ora:member_level:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ora:member_level:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ora:member_level:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ora:member_level:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="member_levelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
<!--      <el-table-column label="策略标识" align="center" prop="strategySign" />-->
      <el-table-column label="会员金额" align="center" prop="levelFee" />
      <el-table-column label="币种" align="center" prop="currency">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ora_sys_currency" :value="scope.row.currency"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="可用状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="会员时长" align="center" prop="memberTime" />
      <el-table-column label="位置" align="center" prop="position" />
      <el-table-column label="排序" align="center" prop="orderBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ora:member_level:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ora:member_level:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员等级配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="策略标识" prop="strategySign">
          <el-input v-model="form.strategySign" placeholder="请输入策略标识" />
        </el-form-item>
        <el-form-item label="会员金额" prop="levelFee">
          <el-input v-model="form.levelFee" placeholder="请输入会员金额" />
        </el-form-item>
        <el-form-item label="币种" prop="currency">
          <el-select v-model="form.currency" placeholder="请选择币种">
            <el-option
              v-for="dict in dict.type.ora_sys_currency"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="可用状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择可用状态">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员时长" prop="memberTime">
          <el-input v-model="form.memberTime" placeholder="请输入会员时长" />
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="排序" prop="orderBy">
          <el-input v-model="form.orderBy" placeholder="请输入排序" />
        </el-form-item>
        <el-divider content-position="center">会员等级配置多语言信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMemberLevelDesc">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMemberLevelDesc">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="memberLevelDescList" :row-class-name="rowMemberLevelDescIndex" @selection-change="handleMemberLevelDescSelectionChange" ref="memberLevelDesc">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="等级名称" prop="levelName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.levelName" placeholder="请输入等级名称" />
            </template>
          </el-table-column>
          <el-table-column label="等级标题" prop="levelTitle" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.levelTitle" placeholder="请输入等级标题" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="语言" prop="language" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.language" placeholder="请选择语言">
                <el-option
                  v-for="dict in dict.type.biz_system_language"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember_level, getMember_level, delMember_level, addMember_level, updateMember_level } from "@/api/ora/member_level";

export default {
  name: "Member_level",
  dicts: ['biz_system_language', 'ora_sys_currency', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMemberLevelDesc: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员等级配置表格数据
      member_levelList: [],
      // 会员等级配置多语言表格数据
      memberLevelDescList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        strategySign: null,
        levelFee: null,
        currency: null,
        status: null,
        memberTime: null,
        position: null,
        orderBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员等级配置列表 */
    getList() {
      this.loading = true;
      listMember_level(this.queryParams).then(response => {
        this.member_levelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        strategySign: null,
        levelFee: null,
        currency: null,
        remark: null,
        status: null,
        memberTime: null,
        position: null,
        orderBy: null
      };
      this.memberLevelDescList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员等级配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember_level(id).then(response => {
        this.form = response.data;
        this.memberLevelDescList = response.data.memberLevelDescList;
        this.open = true;
        this.title = "修改会员等级配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.memberLevelDescList = this.memberLevelDescList;
          if (this.form.id != null) {
            updateMember_level(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember_level(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员等级配置编号为"' + ids + '"的数据项？').then(function() {
        return delMember_level(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 会员等级配置多语言序号 */
    rowMemberLevelDescIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 会员等级配置多语言添加按钮操作 */
    handleAddMemberLevelDesc() {
      let obj = {};
      obj.levelName = "";
      obj.levelTitle = "";
      obj.remark = "";
      obj.language = "";
      this.memberLevelDescList.push(obj);
    },
    /** 会员等级配置多语言删除按钮操作 */
    handleDeleteMemberLevelDesc() {
      if (this.checkedMemberLevelDesc.length == 0) {
        this.$modal.msgError("请先选择要删除的会员等级配置多语言数据");
      } else {
        const memberLevelDescList = this.memberLevelDescList;
        const checkedMemberLevelDesc = this.checkedMemberLevelDesc;
        this.memberLevelDescList = memberLevelDescList.filter(function(item) {
          return checkedMemberLevelDesc.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMemberLevelDescSelectionChange(selection) {
      this.checkedMemberLevelDesc = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ora/member_level/export', {
        ...this.queryParams
      }, `member_level_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
